#include "register.h"
#include "ui_register.h"

Register::Register(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Register)
{
    ui->setupUi(this);
    //sql语句的查找和插入，不需要模型model
}

Register::~Register()
{
    delete ui;
}

void Register::on_pushButton_clicked()
{
    QString userName=this->ui->userName->text();
    QString password=this->ui->password->text();
    QString surePassword=this->ui->sure->text();//确认密码
    QSqlQuery query;
    query.exec("select 账号 from user where 账号 ='"+userName+"'");
    QString str1;
    while(query.next())
    {
        str1 = query.value("账号").toString();
    }

    if (userName.isEmpty()||password.isEmpty()||surePassword.isEmpty())
    {
        QMessageBox::information(NULL, "提示", "账号密码确认密码不得为空！", QMessageBox::Ok);
    }
    else if(!str1.isEmpty())
    {
        QMessageBox::information(NULL, "提示", "该账号已被注册，请换一个账号！", QMessageBox::Ok);
        this->ui->password->clear();
        this->ui->sure->clear();
        this->ui->userName->clear();
        this->ui->userName->setFocus();
    }
    else if(password!=surePassword)
    {
        QMessageBox::information(NULL, "提示", "密码和确认密码不同，请重新输入！", QMessageBox::Ok);
        this->ui->password->clear();
        this->ui->sure->clear();
        this->ui->password->setFocus();
    }
    else
    {
        bool test=query.exec("INSERT INTO user VALUES('"+userName+"','"+password+"')");
        if(test==true)
        {
            QMessageBox::information(NULL, "提示", "注册成功！", QMessageBox::Ok);
            this->close();
        }
        else
        {
            QMessageBox::information(NULL, "提示", "注册不成功，请重新注册！", QMessageBox::Ok);
        }
    }
}
